﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Autohosted_TestWeb
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            Uri redirectUrl;
            switch (SharePointContextProvider.CheckRedirectionStatus(Context, out redirectUrl))
            {
                case RedirectionStatus.Ok:
                    return;
                case RedirectionStatus.ShouldRedirect:
                    Response.Redirect(redirectUrl.AbsoluteUri, endResponse: true);
                    break;
                case RedirectionStatus.CanNotRedirect:
                    Response.Write("An error occurred while processing your request.");
                    Response.End();
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // The following code gets the client context and Title property by using TokenHelper.
                // To access other properties, the app may need to request permissions on the host web.
                SharePointContext spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

                try
                {
                    ViewState["ConnectionString"] = spContext.GetDatabaseConnectionString();
                }
                catch (Exception ex)
                {
                    lblError.Text += ex.Message;
                }

                string info = "";

                lblInfo.Text += "<br/>Request.Url:" + Request.Url.ToString();

                try
                {
                    lblInfo.Text += "<br/>ConnectionString Database:" + ViewState["ConnectionString"].ToString();
                }
                catch (Exception)
                {
                }

                try
                {

                    //info += "<br/>HardwareInfo.GetPhysicalMemory:" + HardwareInfo.GetPhysicalMemory();
                    //info += "<br/>HardwareInfo.GetCpuSpeedInGHz:" + HardwareInfo.GetCpuSpeedInGHz();
                    //lblInfo.Text = info;
                }
                catch (Exception ex)
                {
                    lblError.Text += ex.Message;
                }
                try
                {

                    using (var clientContext = spContext.CreateUserClientContextForSPHost())
                    {
                        clientContext.Load(clientContext.Web, web => web.Title);
                        clientContext.ExecuteQuery();

                        lblInfo.Text += "<br/> Hostweb Title:" + clientContext.Web.Title;
                    }
                }
                catch (Exception ex)
                {

                    lblError.Text += ex.Message;
                }
            }
        }

        protected void btnFillDatabase_Click(object sender, EventArgs e)
        {
            try
            {

                using (SqlConnection cn = new SqlConnection(ViewState["ConnectionString"].ToString()))
                {
                    cn.Open();
                    String sql = String.Format(@"
                INSERT INTO [dbo].[BigTable]
                ([Data])
                VALUES
                ('{0}')", "1234567890");

                    SqlCommand cmd = new SqlCommand(sql, cn);

                    for (int i = 0; i < System.Convert.ToInt64(txtNumberOfRecords.Text); i++)
                    {
                        cmd.ExecuteNonQuery();
                    }

                    SqlCommand cmdReader = new SqlCommand("SELECT Count(*) FROM [dbo].[BigTable]");
                    Int64 count = (Int64)cmdReader.ExecuteScalar();
                    if (count != null)
                    {
                        litResults.Text += "<br/>Count:" + count.ToString();
                    }
                    else
                    {
                        litResults.Text += "<br/>Count:-1";

                    }

                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }

        }



    }
}